package com.evil.common.application.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * 应用拓展类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ExpandTypeEnum {

    /**
     * 应用拓展类型
     */
    TASK("task", "任务"),
    FOLDER("folder", "文件夹"),
    ;

    @Getter
    private final String type;
    @Getter
    private final String name;

    public static Optional<ExpandTypeEnum> findByType(String type) {
        if (StringUtils.isBlank(type)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type.equals(type)).findFirst();
    }

    public static ExpandTypeEnum isExistByType(String type) {
        return ExpandTypeEnum.findByType(type).orElseThrow(ExpandTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<ExpandTypeEnum> action) {
        Arrays.asList(values()).forEach(action);
    }

    public static Stream<ExpandTypeEnum> stream() {
        return Arrays.stream(values());
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "应用拓展类型参数错误");
    }
}
